package com.huluobo.high3demo.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.ActivityLoginBinding;
import com.huluobo.high3demo.databinding.ActivityLoginDemoBinding;
import com.huluobo.mvvm.baen.UserBeanResult;

public class LoginDemoActivity extends AppCompatActivity {
    LoginDemoViewModel loginDemoViewModel;

    ActivityLoginBinding activityLoginBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLoginBinding =DataBindingUtil.setContentView(this,R.layout.activity_login);
        loginDemoViewModel = new ViewModelProvider(this).get(LoginDemoViewModel.class);

        activityLoginBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = activityLoginBinding.edLoginUsername.getText().toString();
                String password = activityLoginBinding.edLoginPassword.getText().toString();
                loginDemoViewModel.login(username, password);
            }
        });

        loginDemoViewModel.success.observe(this, new Observer<UserBeanResult.UserBean>() {
            @Override
            public void onChanged(UserBeanResult.UserBean userBean) {
                ToastUtils.showLong(userBean.getNickname());
            }
        });

        loginDemoViewModel.failed.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }
}